/*请求url*/
var urlData = require('../../data/url.js');

Page({ 
	data: { 
		user: '', 
		psw:'',
    	loginUrl: urlData.urlList.login.url
	},
	/*
		获取输入账号
	*/ 
	userInput :function (e) { 
		this.setData({ 
			user:e.detail.value 
		}) 
	}, 
	/*
		获取输入密码
	*/ 
    pswInput :function (e) { 
  	    this.setData({ 
  	    	psw:e.detail.value 
  	    }) 
    }, 
  	/*
  		登录
  	*/ 
    login: function (e) { 
	    if(this.data.user.length == 0 || this.data.psw.length == 0){
		    wx.showModal({
			    title: '登录提示',
			    content: '用户名或者密码不能为空！',
			    success: function(res) {
				    if (res.confirm) {
				        console.log('用户点击确定')
				    } else if (res.cancel) {
				        console.log('用户点击取消')
				    }
				}
			})
		}else{
			this.sendLoginData();
		} 
	},
	/*
		发送登录数据
	*/
	sendLoginData:function(e){
		var that = this;
		wx.request({
      		url: this.data.loginUrl, 
      		'content-type':'application/json',
		    method:"get",
		    data: {
		    	user:this.data.user,
		    	psw:this.data.psw
		    }, 
		    header: {'content-type': 'application/x-www-form-urlencoded'},  
		    success:function(res) {
		        if (res.data.code == 0){
		        	wx.switchTab({
				  	    url: '../index/index'
				  	});
		        	wx.setStorage({//写入本地数据缓存
					    key:"loginConf",
					    data:{
					    	userId:res.data.id,
			        		userRole:res.data.role,
			        		userName:res.data.name,
			        		userGrade:res.data.grade
					    }
					});
		        }else{
		        	wx.showModal({
					    title: '登录提示',
					    content: '用户名或者密码错误！',
					    success: function(res) {
					        if (res.confirm) {
					            console.log('用户点击确定')
					        } else if (res.cancel) {
					            console.log('用户点击取消')
					        }
					    }
					})
		        }
		    },  
		    fail:function(res){  
		        console.log('登录失败');  
		    }
		});
	}
})